#include <myhead.h>
#include "myprotocol.h"
#include "cgic.h"
#define MYPATH "/home/linux"
// char ID[20] = {0};
// char PS[20] = {0};
// char id[20] = "admin";
// char ps[20] = "888888";

int key;
int msgid;
myprotocol_t buf;

// int semid;

// char tem[32];
// char hum[32];
// char light[32];

long myuid;

char temp_up[32] = {0};
char temp_low[32] = {0};
char hum_up[32] = {0};
char hum_low[32] = {0};
char illu_up[32] = {0};
char illu_low[32] = {0};

char clean_buf[1024] = {0};
void alarm_handler(int arg)
{
    // 非阻塞的方式读走消息队列中的消息
    msgrcv(msgid, clean_buf, sizeof(clean_buf), buf.msgtype, IPC_NOWAIT);

    printf("Content-type: text/html;charset=\"UTF-8\"\n\n");
    printf("<!DOCTYPE html>");
    printf("<html>");
    printf("<body>");
    printf("<center>");
    printf("<h1>网络似乎不太好</h1>"); // 输出错误信息( 大标题 )
    printf("<br>");                    // 换行
    // printf("<a href=\"/index.html\">返回重新输入</a>");
    printf("</center>");
    printf("</body>");
    printf("</html>");
    exit(-1);
}
int cgiMain(int argc, const char *argv[])
{
    signal(SIGALRM, alarm_handler);
    myuid = 0;

    key = ftok(MYPATH, 'A');
    if (-1 == key)
    {
        perror("ftok error");
        return -1;
    }
    msgid = msgget(key, IPC_CREAT | 0666);
    if (-1 == msgid)
    {
        perror("msgget error");
        return -1;
    }

    memset(&buf, 0, sizeof(buf));

    char cp_res[32] = {0};
    char cookiebuf[20] = {0};
    memcpy(cookiebuf, cgiCookie, 20);

    char *temp = cookiebuf;
    int i = 0;
    while (*temp)
    {
        if (*temp == '=')
        {
            temp++;
            while (*temp)
            {
                cp_res[i] = *temp;
                i++;
                temp++;
            }
            break;
        }
        temp++;
    }
    cp_res[i] = '\0';

    temp = cp_res;
    while (*temp)
    {
        myuid += *temp;
        temp++;
    }
    buf.uid = myuid;
    buf.msgtype = myuid;
    buf.cmd = SET_THRESHOLD_REQ;

    cgiFormString("temp_up", temp_up, 32);
    cgiFormString("temp_low", temp_low, 32);
    cgiFormString("hum_up", hum_up, 32);
    cgiFormString("hum_low", hum_low, 32);
    cgiFormString("illu_up", illu_up, 32);
    cgiFormString("illu_low", illu_low, 32);

    buf.threshold_info.tem_max = atof(temp_up);
    buf.threshold_info.tem_min = atof(temp_low);
    buf.threshold_info.hum_max = atof(hum_up);
    buf.threshold_info.hum_min = atof(hum_low);
    buf.threshold_info.lx_max = atof(illu_up);
    buf.threshold_info.lx_min = atof(illu_low);

    if (-1 == msgsnd(msgid, &buf, sizeof(buf) - sizeof(buf.msgtype), 0))
    {
        perror("msgsnd error");
        return -1;
    }
    alarm(4);
    // 收过来就算不用也要收 保持队列清空
    if (-1 == msgrcv(msgid, &buf, sizeof(buf) - sizeof(buf.msgtype), myuid * 2, 0))
    {
        perror("msgrcv error");
        return -1;
    }

    printf("Content-type: text/html;charset=\"UTF-8\"\n\n"); // 固定格式 必须要加
    printf("<!DOCTYPE html>");
    printf("<html>");
    printf("<body style=\"display: flex; justify-content: center; align-items: center; height: 100vh; margin: 0;\">");
    printf("<div style=\"margin-top: -500px;\">");
    printf("<a href=\"/home/living_room.html\" style=\"font-size:48px;\">前往客厅</a>");
    printf("</div>");
    printf("</body>");
    printf("</html>");

    return 0;
}